<template>
	<view class="container">
		<z-paging ref="paging" v-model="addressList" @query="queryList" :flex="true" empty-view-text="暂无数据~"
			empty-view-img="/static/empty.png" :empty-view-img-style="imgStyle" :paging-style="pagingStyle">
			<view slot="top">
				<u-navbar height="50" back-icon-color="#333" title="收货地址" title-color="#333" title-size="36">
				</u-navbar>
			</view>
			<view class="centerView">
				<view class="addrListView" v-for="(item,index) in addressList" :key="index">
					<view class="addressView">
						<text class="addressTitle">{{item.contact_person}}</text>
						<text class="addrPhone">{{item.contact_phone}}</text>
						<view class="addressEdit">
							<image class="delAddressIcon" src="/static/edit.png" @click.stop="onDetails(item)"></image>
							<image class="editAddressIcon" src="/static/del.png" @click.stop="onDetele(item)"></image>
						</view>
					</view>
					<view class="editAddrView">
						<view class="addrType" v-if="item.is_default == 1">默认</view>
						<view class="address">{{item.address}}</view>
					</view>
				</view>
			</view>
		</z-paging>
		<view class="addAddress" @click="onNewAddress()">+ 新建收货地址</view>
		<u-modal v-model="deleteDialog" :content="content" :show-cancel-button="true" confirm-color="#F6632D"
			@confirm="onDelConfirm"></u-modal>
	</view>
</template>

<script>
	import { getAddressesList,delAddress } from '@/api/user.js'
import toast from '../../uni_modules/vk-uview-ui/libs/function/toast';
	export default {
		data() {
			return {
				imgStyle: {
					width: '12rem'
				},
				pagingStyle: {
					paddingBottom: '130rpx'
				},
				deleteDialog: false,
				content: '确定删除该地址吗？',
				address_id: '',
				addressList: []
			}
		},
		onShow() {
			let self = this;
			this.$nextTick(function() {
				self.$refs.paging.reload();
			});
		},
		methods: {
			onNewAddress() {
				// this.$utils.jump();
				uni.navigateTo({
					url:'/other/add-address/add-address?type=0'
				})
			},
			queryList(page, pageSize) {
				let self = this;
				getAddressesList().then(res=>{
					self.$refs.paging.complete(res.data);
				}).catch(err=>{
					self.$refs.paging.complete();
				})
			},
			onDetails(item) {
				// this.$utils.jump();
				uni.navigateTo({
					url:'/other/add-address/add-address?type=1&id=' + item.id
				})
			},
			onDetele(item) {
				this.address_id = item.id;
				this.deleteDialog = true;
			},
			onDelConfirm() {
				let self = this;
				delAddress({delivery_address_id: this.address_id}).then(res=>{
					toast('删除成功')
					setTimeout(function() {
						self.queryList();
					}, 500);
				}).catch()
			}
		}
	}
</script>

<style>
	@import url('delivery-address.css');

	page {
		background: #f5f5f5;
	}
</style>
